/**
 * Created by zhanglihe on 16/1/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'newone_groups',
                model: {
                    url: {
                        delete: _global.url.api + 'newone_group/{id}'
                    },
                    event: {
                        '.item-edit': 'edit',
                        '.item-remove': 'remove'
                    },
                    edit: function (e) {
                        var __this = this;
                        e.preventDefault();
                        dialog({
                            title: '修改：' + __this.data.name,
                            width: 800,
                            content: template('add-tpl', __this.data),
                            ok: function () {
                                var data = {
                                    id: __this.data.id,
                                    user_tag_id: $('[name="user_tag_id"]').val(),
                                    new_user_card_image: $('[name="new_user_card_image"]').val(),
                                    old_user_card_image: $('[name="old_user_card_image"]').val(),
                                    new_user_top_image: $('[name="new_user_top_image"]').val(),
                                    old_user_top_image: $('[name="old_user_top_image"]').val(),
                                    new_user_foot_image: $('[name="new_user_foot_image"]').val(),
                                    old_user_foot_image: $('[name="old_user_foot_image"]').val(),
                                    user_share_data_title: $('[name="user_share_data_title"]').val(),
                                    user_share_data_content: $('[name="user_share_data_content"]').val(),
                                    user_share_data_image: $('[name="user_share_data_image"]').val(),
                                    user_share_image: $('[name="user_share_image"]').val(),
                                    share_inner_id: $('[name="share_inner_id"]').val(),
                                    share_outer_id: $('[name="share_outer_id"]').val(),
                                };
                                if (data.share_outer_id=='' || data.share_inner_id=='' || isNaN(data.share_outer_id) || isNaN(data.share_inner_id) || (data.id > 3 && !data.user_tag_id) || !data.new_user_card_image || !data.old_user_card_image || !data.new_user_top_image || !data.old_user_top_image || !data.new_user_foot_image || !data.old_user_foot_image || !data.user_share_image) {
                                    Alert({'msg': '请正确填写', 'type': 'error'});
                                    return false;
                                }
                                if (new Model({
                                        url: _global.url.api + 'newone_group',
                                        data: data
                                    }).post()) {
                                    _this.collection.reload();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            }
                        }).show();
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '删除后无法恢复(同时删除相关商品关系)，确定要删除吗?',
                            ok: function () {
                                _this.del();
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();//移除加载效果
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            $('#btn-add').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title: '增加',
                    width: 800,
                    content: template('add-tpl', {}),
                    ok: function () {
                        var data = {
                            user_tag_id: $('[name="user_tag_id"]').val(),
                            new_user_card_image: $('[name="new_user_card_image"]').val(),
                            old_user_card_image: $('[name="old_user_card_image"]').val(),
                            new_user_top_image: $('[name="new_user_top_image"]').val(),
                            old_user_top_image: $('[name="old_user_top_image"]').val(),
                            new_user_foot_image: $('[name="new_user_foot_image"]').val(),
                            old_user_foot_image: $('[name="old_user_foot_image"]').val(),
                            user_share_data_title: $('[name="user_share_data_title"]').val(),
                            user_share_data_content: $('[name="user_share_data_content"]').val(),
                            user_share_data_image: $('[name="user_share_data_image"]').val(),
                            user_share_image: $('[name="user_share_image"]').val(),
                            share_inner_id: $('[name="share_inner_id"]').val(),
                            share_outer_id: $('[name="share_outer_id"]').val(),
                        };
                        if (data.share_outer_id=='' || data.share_inner_id=='' || isNaN(data.share_outer_id) || isNaN(data.share_inner_id) || !data.user_tag_id || !data.new_user_card_image || !data.old_user_card_image || !data.new_user_top_image || !data.old_user_top_image || !data.new_user_foot_image || !data.old_user_foot_image || !data.user_share_image) {
                            Alert({'msg': '请正确填写', 'type': 'error'});
                            return false;
                        }
                        if (new Model({
                                url: _global.url.api + 'newone_group',
                                data: data
                            }).post()) {
                            _this.collection.reload();
                            return true;
                        }
                        return false;
                    }, cancel: function () {
                    }
                }).show();
            });

            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                var item_id = $(this).attr('data-id');
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img, item_id);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },
        imgPreview: function (img, item_id) {
            if (typeof img.id !== 'undefined') {
                img.item_id = item_id;
                $('#' + item_id).html(template('img-tpl', img));
            }
        }
    };
    new index();
});